package com.hroze.util.web;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;

/**
 * @author 王志湖 E-mail:695w@163.com
 * @version 创建时间：2010-1-24 上午05:09:46 类说明 生成首页数据
 */
public class SystemUtil {
	private void autoCreate() {
		System.out.println("更新首页数据....");
		HttpClient client = new HttpClient();
		String header = "<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"GBK\"%>";
		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
				"UTF-8");
		for (String page : SystemConfig.CONFIG.getArray("PAGES")) {
			String uri = SystemConfig.getURL() + "/" + page + ".json";
			HttpMethod method = new GetMethod(uri);
			try {
				client.executeMethod(method);
				InputStream is = method.getResponseBodyAsStream();
				String body = IOUtils.toString(is, "UTF-8");
				IOUtils.closeQuietly(is);
				method.releaseConnection();
				File f = new File(SystemConfig.getRealPath("/" + page + ".jsp"));
				if (f.exists()) {
					f.delete();
				}
				BufferedWriter output = new BufferedWriter(new FileWriter(f));
				output.write(header);
				output.write(body);
				output.close();
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
